package cn.yuanqiao.cqbc.timedTasks.taskClass.taskListService.impl;


import cn.yuanqiao.cqbc.fileBackup.domain.Filebackup;
import cn.yuanqiao.cqbc.fileBackupLog.domain.FileBackupLog;
import cn.yuanqiao.cqbc.fileBackupLog.mapper.FileBackupLogMapper;
import cn.yuanqiao.cqbc.fileBackupLog.service.IFileBackupLogService;
import cn.yuanqiao.cqbc.timedTasks.taskClass.taskListService.TaskListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("taskList")
public class TaskListTestTwoServiceImpl implements TaskListService {

    @Resource
    private FileBackupLogMapper filebackupLogMapper;
    @Autowired
    private IFileBackupLogService filebackupLogService;
    @Override
    public <T> Map<String,Object> task(T ...args)  {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String beginTime=sdf.format(new Date());
        Filebackup param=(Filebackup)args[0];
        //开始备份
         String backupMethod=param.getBackupMethod();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","200");
        //判断数据库里是否有这条数据没有插入日志，有的话更新
        FileBackupLog filebackupLog=filebackupLogMapper.getFilebackupLogByFilebackupId(param.getId());
         try {
             if ("1".equals(backupMethod)) {
                 //开始备份
                 this.copy(new File(param.getOriginalAddress()), new File(param.getBackupAddress()),param.getOriginalAddress());
             }else if("0".equals(backupMethod)){

                 this.incrementCopy(new File(param.getOriginalAddress()), new File(param.getBackupAddress()),param.getOriginalAddress());

             }
         }catch (Exception ex){
             ex.printStackTrace();
             map.put("code","500");
         }

        String endTime=sdf.format(new Date());
        if(filebackupLog==null){
            filebackupLog=new FileBackupLog();
            filebackupLog.setFilebackupId(param.getId());
            filebackupLog.setBeginTime(beginTime);
            filebackupLog.setEndTime(endTime);

            filebackupLog.setCreateTime(sdf.format(new Date()));
            filebackupLog.setTenantCode(param.getTenantCode());
            filebackupLogService.insertFileBackupLog(filebackupLog);
        }else{
            filebackupLog.setBeginTime(beginTime);
            filebackupLog.setEndTime(endTime);
            filebackupLogService.updateFileBackupLog(filebackupLog);
        }
        return map;//"this is the two two task list 2 !"+s+":::::"+s1;
    }


    public  void copy(File originalFile, File backupFile,String originalAddress) throws IOException {
        if (!backupFile.exists()) {
            backupFile.mkdirs();
        }
        if (originalFile.isDirectory()) {
            File[] files = originalFile.listFiles();
            for (File file2 : files) {
                if (file2.isDirectory()) {
                    copy(file2, new File(backupFile.getPath(), file2.getName()),originalAddress);
                } else {
                    Files.copy(file2.toPath(), Paths.get(backupFile.getPath()+File.separator+file2.getName() ),StandardCopyOption.REPLACE_EXISTING);
                }

            }

        }
    }
    public  void incrementCopy(File originalFile, File backupFile,String originalAddress) throws IOException {
        if (!backupFile.exists()) {
            backupFile.mkdirs();
        }
        if (originalFile.isDirectory()) {
            File[] files = originalFile.listFiles();
            for (File file2 : files) {
                if (file2.isDirectory()) {
                    incrementCopy(file2, new File(backupFile.getPath(), file2.getName()),originalAddress);
                } else {
                    //判断文件是否存在如果存在不在备份
                    File file=new File(backupFile.getPath() + file2.getPath().substring(originalAddress.length()));
                    if(!file.exists()||file2.length()!=file.length()) {
                        Files.copy(file2.toPath(), Paths.get(backupFile.getPath()+File.separator+file2.getName() ), StandardCopyOption.REPLACE_EXISTING);
                       }
                    }

            }

        }
    }



}
